package com.hust.yuqian.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by mmutuyu on 14-1-11.
 */
public class MakeWaterMarkPic {

    public static final String WATERMARK_NAME  = "yuqian.jpg";//水印名称

    /**
     * 会在程序当前路径处生成一个32*32的二值图片作为水印
     * @param info 水印信息
     */
    public static void makePic(String info){
        int width = UsefulData.WATERMARK_WIDTH;
        int height = UsefulData.WATERMARK_HEIGHT;
        String name = WATERMARK_NAME;

        StringBuilder path = new StringBuilder(System.getProperty("user.dir")).append(System.getProperty("file.separator")).append(name);
        File file = new File(path.toString());
        UsefulData.watermark_path = path.toString();

        Font font = new Font("Serif", Font.LAYOUT_LEFT_TO_RIGHT, 2);
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);//二值化

        Graphics2D g2 = (Graphics2D)bi.getGraphics();
        g2.setBackground(Color.WHITE);
        g2.clearRect(0, 0, width, height);
        g2.setPaint(Color.BLACK);
        FontRenderContext context = g2.getFontRenderContext();
        Rectangle2D bounds = font.getStringBounds(info, context);
        double x = (width - bounds.getWidth()) / 2;
        double y = (height - bounds.getHeight()) / 2;
        double ascent = -bounds.getY();
        double baseY = y + ascent;
        g2.drawString(info, 0, (int)baseY);
        try{
            ImageIO.write(bi, "jpg", file);
        }catch(IOException e){
            System.out.println("makePic wrong!");
        }

    }

    /**
     *
     * @param watermark_extract 提取出来的水印数组
     * @return 提取到的水印图片的BufferedImage
     */
    public static BufferedImage makePic(int [][] watermark_extract){
        int height = UsefulData.WATERMARK_HEIGHT;
        int width = UsefulData.WATERMARK_WIDTH;
        int rgb = 0;
        BufferedImage watermark_extracted = new BufferedImage(UsefulData.WATERMARK_HEIGHT,
                UsefulData.WATERMARK_WIDTH, BufferedImage.TYPE_BYTE_BINARY);
        for(int i = 0; i < height; i++){
            for(int j = 0; j < width; j++){
                rgb = watermark_extract[i][j];
                if(rgb<0){
                    rgb = 0;
                }else{
                    rgb = 16777215;
                }
                watermark_extracted.setRGB(j, i, rgb);
            }
        }
        return watermark_extracted;
    }

    /**
     *
     * @param info 写入的信息
     * @return (-1,1)组成的 W 128*128矩阵
     */
    public static int [][] binaryMatrix(String info){
        //首先生成带有 info 信息的二值水印
        MakeWaterMarkPic.makePic(info);
        ImageTools imageTools = new ImageTools();
        BufferedImage bufferedImage = imageTools.getImage(UsefulData.watermark_path);
        int [][]rgb = imageTools.getImageRGB(bufferedImage);
        //已知水印的大小为128*128
        for(int i = 0; i < UsefulData.WATERMARK_HEIGHT; i++){
            for(int j = 0; j < UsefulData.WATERMARK_WIDTH; j++){
                if(rgb[i][j] > 10000000)rgb[i][j] = 1;//以10000000为分界线
                else{
                    rgb[i][j] = -1;
                }
            }
        }
        return rgb;
    }
}
